//------------------------------------------------------------------------------
//!	@file	ScanArgs.c
//!	@brief
//!		Scan arguments
//!
//!	@Description
//!		Scan arguments from the config file.
//!
//!
//!	Originally Developed: July, 1997 to October, 1999
//!
//!	$Id: ScanArgs.c 125 2013-01-30 23:59:23Z ac.verbeck@gmail.com $
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//	Copyright and Disclaimer Notice
//
//	Copyright (c) 2013 A.C. Verbeck
//
//	This file is part of CreateLUT: A LUT creation library.
//
//	CreateLUT is free software: you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation, either version 3 of the License, or
//	(at your option) any later version.
//
//	CreateLUT is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with CreateLUT.  If not, see <http://www.gnu.org/licenses/>.
//
//------------------------------------------------------------------------------


#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>

#include <stdint.h>
#include <stdbool.h>

#include "ScanArgs.h"

//
//	Local information
//
#define	MAX_LINE	256
#define	DEF_FILE	"CreateLut.cfg"

//
//	Local typedefs
//
typedef void (*FP_PARAM_T)(char* Val);
typedef struct PARAM_LISTtag {
	char*		Str;
	FP_PARAM_T	Fn;
} PARAM_LIST;

//
//	Local variables
//
static	char	CurrLine[MAX_LINE];
static	char*	CurrParam;
static	char*	CurrVal;

static void	ParamGmaCt(char* Val);
static void	ParamGmaStart(char* Val);
static void	ParamGmaEnd(char* Val);
static void	ParamGainCt(char* Val);
static void	ParamGainStart(char* Val);
static void	ParamGainEnd(char* Val);
static void	ParamIFile(char* Val);
static void	ParamOFile(char* Val);
static void	ParamProcessCtrl(char* Val);
static void	ParamDebug(char* Val);
static void	ParamDFile(char* Val);
static void	ParamNoDSP(char* Val);

static	PARAM_LIST ParamList[] = {
	{	"LutGammaCt",	ParamGmaCt		},
	{	"StartGamma",	ParamGmaStart	},
	{	"EndGamma",		ParamGmaEnd		},
	{	"LutGainCt",	ParamGainCt		},
	{	"StartGain",	ParamGainStart	},
	{	"EndGain",		ParamGainEnd	},
	{	"InputFile",	ParamIFile		},
	{	"OutputFile",	ParamOFile		},
	{	"ProcessCtrl",	ParamProcessCtrl},
	{	"Debug",		ParamDebug		},
	{	"DebugFile",	ParamDFile		},
	{	"NoDSP",		ParamNoDSP		}
};
#define	PARAM_CT	(sizeof(ParamList)/sizeof(PARAM_LIST))


//
//	Name:
//		ScanArgs
//
//	Description:
//		Reads arguments from a file.
//
//	Parameters:
//		char*	CfgFile:	Configuartion File
//
//	Returns:
//		(None)
//
void ScanArgs(char* CfgFile)
{
	FILE*	f;
	int32_t	i,ArgFound,LineCt;
	char*	tFName;

	tFName = (CfgFile==NULL) ? DEF_FILE : CfgFile;
	f = fopen(tFName,"r");
	if (f == NULL) {
		printf("File open error: %s\n", tFName);
		exit(1);
	}

	LineCt = 0;
	while (fgets(CurrLine, MAX_LINE, f) != NULL) {
		++LineCt;											//	Boost the line counter
		CurrParam = strtok(CurrLine," \t\n");				//	Get the line
		if (CurrParam == NULL) continue;					//	Scan for blank lines
		if (strcmp(CurrParam, "//") == 0)	continue;		//	Scan for comments

		ArgFound = 0;
		for (i=0; i<PARAM_CT; i++) {
			if (strcmp(ParamList[i].Str,CurrParam) == 0) {
				CurrVal = strtok(NULL," \t\n");				//	Get the equal sign
				if (strcmp(CurrVal, "=") != 0) {
					printf("Syntax error: File: %s Line: %d Param: %s\n",tFName,LineCt,CurrParam);
					exit(1);
				}
				CurrVal = strtok(NULL," \t\n");				//	Get the value
				(*ParamList[i].Fn)(CurrVal);
				ArgFound = 1;
				break;
			}
		}
		if (!ArgFound) {
			printf("Unknown parameter: %s\n",CurrParam);
			exit(1);
		}
	}
	fclose(f);
}


//
//	Name:
//		ParamGmaCt
//
//	Description:
//		Reads gamma count into LutGammaCt.
//
//	Parameters:
//		char*	Val:	Value of parameter
//
//	Returns:
//		(None)
//
static void ParamGmaCt(char* Val)
{
	extern int32_t LutGammaCt;

	LutGammaCt	= atoi(Val);
}

//
//	Name:
//		ParamGmaStart
//
//	Description:
//		Reads gamma start into StartGamma.
//
//	Parameters:
//		char*	Val:	Value of parameter
//
//	Returns:
//		(None)
//
static void ParamGmaStart(char* Val)
{
	extern double StartGamma;

	StartGamma = atof(Val);
}

//
//	Name:
//		ParamGmaEnd
//
//	Description:
//		Reads gamma end into EndGamma.
//
//	Parameters:
//		char*	Val:	Value of parameter
//
//	Returns:
//		(None)
//
static void ParamGmaEnd(char* Val)
{
	extern double EndGamma;

	EndGamma = atof(Val);
}


//
//	Name:
//		ParamGainCt
//
//	Description:
//		Reads gain count into LutGainCt.
//
//	Parameters:
//		char*	Val:	Value of parameter
//
//	Returns:
//		(None)
//
static void ParamGainCt(char* Val)
{
	extern int32_t LutGainCt;

	LutGainCt = atoi(Val);
}

//
//	Name:
//		ParamGainStart
//
//	Description:
//		Reads gain start into StartGain.
//
//	Parameters:
//		char*	Val:	Value of parameter
//
//	Returns:
//		(None)
//
static void ParamGainStart(char* Val)
{
	extern double StartGain;

	StartGain = atof(Val);
}

//
//	Name:
//		ParamGainEnd
//
//	Description:
//		Reads gain end into EndGain.
//
//	Parameters:
//		char*	Val:	Value of parameter
//
//	Returns:
//		(None)
//
static void ParamGainEnd(char* Val)
{
	extern double EndGain;

	EndGain = atof(Val);
}


//
//	Name:
//		ParamIFile
//
//	Description:
//		Reads input file into InFileName.
//
//	Parameters:
//		char*	Val:	Value of parameter
//
//	Returns:
//		(None)
//
static void ParamIFile(char* Val)
{
	extern char InFileName[];

	strcpy(InFileName,Val);
}

//
//	Name:
//		ParamOFile
//
//	Description:
//		Reads output file into OutFileName.
//
//	Parameters:
//		char*	Val:	Value of parameter
//
//	Returns:
//		(None)
//
static void ParamOFile(char* Val)
{
	extern char OutFileName[];

	strcpy(OutFileName,Val);
}


//
//	Name:
//		ParamProcessCtrl
//
//	Description:
//		Reads process control into ProcessCtrl.
//
//	Parameters:
//		char*	Val:	Value of parameter
//
//	Returns:
//		(None)
//
static void ParamProcessCtrl(char* Val)
{
	extern uint32_t ProcessCtrl;

	ProcessCtrl = (uint32_t)strtoul(Val, NULL, 0x10);
}

//
//	Name:
//		ParamDebug
//
//	Description:
//		Reads debug info into Debug.
//
//	Parameters:
//		char*	Val:	Value of parameter
//
//	Returns:
//		(None)
//
static void ParamDebug(char* Val)
{
	extern uint32_t Debug;

	Debug |= (uint32_t)strtoul(Val, NULL, 0x10);
}

//
//	Name:
//		ParamDFile
//
//	Description:
//		Places the debug file string into DebugFileName.
//
//	Parameters:
//		char*	Val:	Value of parameter
//
//	Returns:
//		(None)
//
static void ParamDFile(char* Val)
{
	extern char DebugFileName[];

	strcpy(DebugFileName,Val);
}

//
//	Name:
//		ParamNoDSP
//
//	Description:
//		Sets / Clears the NoDSP flag
//
//	Parameters:
//		char*	Val:	Value of parameter
//
//	Returns:
//		(None)
//
static void ParamNoDSP(char* Val)
{
	extern bool NoDSP;

	NoDSP = (Val[0] == '1') ? true : false;
}

//
//	End: ScanArgs.h
//
